package com.mxchip.livestarmobile.mobile.db;

import android.content.Context;

import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.room.TypeConverters;
import androidx.room.migration.Migration;
import androidx.sqlite.db.SupportSQLiteDatabase;

import com.mxchip.livestarmobile.mobile.db.dao.CallLogDao;
import com.mxchip.livestarmobile.mobile.db.dao.CallLogWithContactDao;
import com.mxchip.livestarmobile.mobile.db.dao.ContactDao;
import com.mxchip.livestarmobile.mobile.db.dao.AccountDao;
import com.mxchip.livestarmobile.mobile.db.entity.CallLog;
import com.mxchip.livestarmobile.mobile.db.entity.Contact;
import com.mxchip.livestarmobile.mobile.db.entity.Account;


@Database(entities = {Contact.class, CallLog.class, Account.class}, version = 2)
@TypeConverters(Converters.class)
public abstract class AppDatabase extends RoomDatabase {

    public abstract ContactDao contactDao();
    public abstract CallLogDao calllogDao();
    public abstract AccountDao accountDao();
    public abstract CallLogWithContactDao CallLogWithContactDao();


    private static AppDatabase sInstance;

    private static final String DATABASE_NAME = "hxb-mobile-db";

    public static AppDatabase getInstance(final Context context) {
        if (sInstance == null) {
            synchronized (AppDatabase.class) {
                if (sInstance == null) {
                    sInstance = Room.databaseBuilder(context, AppDatabase.class, DATABASE_NAME)
                            // .allowMainThreadQueries()
                            .addMigrations(MIGRATION_1_2)
                            .build();
                }
            }
        }
        return sInstance;
    }

    static final Migration MIGRATION_1_2 = new Migration(1, 2) {
        @Override
        public void migrate(SupportSQLiteDatabase database) {
            database.execSQL("ALTER TABLE Account ADD COLUMN room_id TEXT");
            database.execSQL("ALTER TABLE Account ADD COLUMN number TEXT");
        }
    };
}